package conf

import (
	"github.com/spf13/viper"
	"log"
)

type Config struct {
	Sql struct {
		Type     string `yaml:"Type"`     //数据库类型 mysql mssql sqliet
		DataBase string `yaml:"DataBase"` //数据库名
		Port     int    `yaml:"Port"`     //端口
		User     string `yaml:"User"`     //用户名
		Pass     string `yaml:"Pass"`     //密码
		Host     string `yaml:"Host"`     //地址
		LogMode  bool   `yaml:"LogMode"`  //是否打印sql语句 true打印 ，false不打印
	} `yaml:"MySql"`
	Server struct {
		Port    string `yaml:"Port"`
		SetMode bool   `yaml:"SetMode"`
	} `yaml:"Server"`
	LogConfig struct {
		Filename   string `yaml:"Filename"`
		MaxSize    int    `yaml:"MaxSize"`
		MaxBackups int    `yaml:"MaxBackups"`
		MaxAge     int    `yaml:"MaxAge"`
	} `yaml:"LogConfig"`
}

func InitConfig() (*Config, error) {
	var _config *Config
	//读取配置文件
	viper.SetConfigType("yaml")
	viper.SetConfigFile("./conf/config.yml")
	err := viper.ReadInConfig()
	if err != nil {
		log.Println("配置文件初始化失败 err:", err)
		return _config, err
	}

	err = viper.Unmarshal(&_config)
	if err != nil {
		log.Println("配置文件初始化失败 err:", err)
		return _config, err
	}
	return _config, nil

}
